<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="作品方向" prop="workDirection">
        <el-select v-model="queryParams.workDirection" placeholder="请选择作品方向" clearable size="small">
          <el-option
            v-for="dict in workDirectionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小说名" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入小说名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者名" prop="authorName">
        <el-input
          v-model="queryParams.authorName"
          placeholder="请输入作者名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="爬虫源站ID" prop="crawlSourceId">
        <el-input
          v-model="queryParams.crawlSourceId"
          placeholder="请输入爬虫源站ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抓取的源站小说ID" prop="crawlBookId">
        <el-input
          v-model="queryParams.crawlBookId"
          placeholder="请输入抓取的源站小说ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['book:book:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['book:book:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['book:book:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['book:book:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="作品方向" align="center" prop="workDirection" :formatter="workDirectionFormat" />
      <el-table-column label="小说名" align="center" prop="bookName" />
      <el-table-column label="作者名" align="center" prop="authorName" />
      <el-table-column label="点击量" align="center" prop="visitCount" />
      <el-table-column label="总字数" align="center" prop="wordCount" />
      <el-table-column label="是否收费" align="center" prop="isVip" :formatter="isVipFormat" />
      <el-table-column label="是否停更" align="center" prop="crawlIsStop" :formatter="crawlIsStopFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['book:book:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['book:book:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小说对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="作品方向" prop="workDirection">
          <el-select v-model="form.workDirection" placeholder="请选择作品方向">
            <el-option
              v-for="dict in workDirectionOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类ID" prop="catId">
          <el-input v-model="form.catId" placeholder="请输入分类ID" />
        </el-form-item>
        <el-form-item label="分类名" prop="catName">
          <el-input v-model="form.catName" placeholder="请输入分类名" />
        </el-form-item>
        <el-form-item label="小说封面" prop="picUrl">
          <el-input v-model="form.picUrl" placeholder="请输入小说封面" />
        </el-form-item>
        <el-form-item label="小说名" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入小说名" />
        </el-form-item>
        <el-form-item label="作者id，预留字段" prop="authorId">
          <el-input v-model="form.authorId" placeholder="请输入作者id，预留字段" />
        </el-form-item>
        <el-form-item label="作者名" prop="authorName">
          <el-input v-model="form.authorName" placeholder="请输入作者名" />
        </el-form-item>
        <el-form-item label="书籍描述" prop="bookDesc">
          <el-input v-model="form.bookDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="评分，预留字段" prop="score">
          <el-input v-model="form.score" placeholder="请输入评分，预留字段" />
        </el-form-item>
        <el-form-item label="书籍状态，0：连载中，1：已完结" prop="bookStatus">
          <el-select v-model="form.bookStatus" placeholder="请选择书籍状态，0：连载中，1：已完结">
            <el-option
              v-for="dict in bookStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="点击量" prop="visitCount">
          <el-input v-model="form.visitCount" placeholder="请输入点击量" />
        </el-form-item>
        <el-form-item label="总字数" prop="wordCount">
          <el-input v-model="form.wordCount" placeholder="请输入总字数" />
        </el-form-item>
        <el-form-item label="评论数" prop="commentCount">
          <el-input v-model="form.commentCount" placeholder="请输入评论数" />
        </el-form-item>
        <el-form-item label="最新目录ID" prop="lastIndexId">
          <el-input v-model="form.lastIndexId" placeholder="请输入最新目录ID" />
        </el-form-item>
        <el-form-item label="最新目录名" prop="lastIndexName">
          <el-input v-model="form.lastIndexName" placeholder="请输入最新目录名" />
        </el-form-item>
        <el-form-item label="最新目录更新时间" prop="lastIndexUpdateTime">
          <el-date-picker clearable size="small"
            v-model="form.lastIndexUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最新目录更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否收费" prop="isVip">
          <el-select v-model="form.isVip" placeholder="请选择是否收费">
            <el-option
              v-for="dict in isVipOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上架状态" prop="shelvesStatus">
          <el-select v-model="form.shelvesStatus" placeholder="请选择上架状态">
            <el-option
              v-for="dict in shelvesStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="爬虫源站ID" prop="crawlSourceId">
          <el-input v-model="form.crawlSourceId" placeholder="请输入爬虫源站ID" />
        </el-form-item>
        <el-form-item label="抓取的源站小说ID" prop="crawlBookId">
          <el-input v-model="form.crawlBookId" placeholder="请输入抓取的源站小说ID" />
        </el-form-item>
        <el-form-item label="最后一次的抓取时间" prop="crawlLastTime">
          <el-date-picker clearable size="small"
            v-model="form.crawlLastTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后一次的抓取时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否停更" prop="crawlIsStop">
          <el-select v-model="form.crawlIsStop" placeholder="请选择是否停更">
            <el-option
              v-for="dict in crawlIsStopOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBook, getBook, delBook, addBook, updateBook, exportBook } from "@/api/book/book";

export default {
  name: "Book",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小说表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 作品方向字典
      workDirectionOptions: [],
      // 书籍状态，0：连载中，1：已完结字典
      bookStatusOptions: [],
      // 是否收费字典
      isVipOptions: [],
      // 上架状态字典
      shelvesStatusOptions: [],
      // 是否停更字典
      crawlIsStopOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workDirection: null,
        bookName: null,
        authorName: null,
        crawlSourceId: null,
        crawlBookId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        picUrl: [
          { required: true, message: "小说封面不能为空", trigger: "blur" }
        ],
        bookName: [
          { required: true, message: "小说名不能为空", trigger: "blur" }
        ],
        authorName: [
          { required: true, message: "作者名不能为空", trigger: "blur" }
        ],
        bookDesc: [
          { required: true, message: "书籍描述不能为空", trigger: "blur" }
        ],
        score: [
          { required: true, message: "评分，预留字段不能为空", trigger: "blur" }
        ],
        bookStatus: [
          { required: true, message: "书籍状态，0：连载中，1：已完结不能为空", trigger: "change" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("work_direction").then(response => {
      this.workDirectionOptions = response.data;
    });
    this.getDicts("book_status").then(response => {
      this.bookStatusOptions = response.data;
    });
    this.getDicts("is_vip").then(response => {
      this.isVipOptions = response.data;
    });
    this.getDicts("shelves_status").then(response => {
      this.shelvesStatusOptions = response.data;
    });
    this.getDicts("crawl_is_stop").then(response => {
      this.crawlIsStopOptions = response.data;
    });
  },
  methods: {
    /** 查询小说列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 作品方向字典翻译
    workDirectionFormat(row, column) {
      return this.selectDictLabel(this.workDirectionOptions, row.workDirection);
    },
    // 书籍状态，0：连载中，1：已完结字典翻译
    bookStatusFormat(row, column) {
      return this.selectDictLabel(this.bookStatusOptions, row.bookStatus);
    },
    // 是否收费字典翻译
    isVipFormat(row, column) {
      return this.selectDictLabel(this.isVipOptions, row.isVip);
    },
    // 上架状态字典翻译
    shelvesStatusFormat(row, column) {
      return this.selectDictLabel(this.shelvesStatusOptions, row.shelvesStatus);
    },
    // 是否停更字典翻译
    crawlIsStopFormat(row, column) {
      return this.selectDictLabel(this.crawlIsStopOptions, row.crawlIsStop);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workDirection: null,
        catId: null,
        catName: null,
        picUrl: null,
        bookName: null,
        authorId: null,
        authorName: null,
        bookDesc: null,
        score: null,
        bookStatus: null,
        visitCount: null,
        wordCount: null,
        commentCount: null,
        lastIndexId: null,
        lastIndexName: null,
        lastIndexUpdateTime: null,
        isVip: null,
        shelvesStatus: null,
        updateTime: null,
        createTime: null,
        crawlSourceId: null,
        crawlBookId: null,
        crawlLastTime: null,
        crawlIsStop: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小说";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBook(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小说";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBook(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除小说编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBook(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有小说数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBook(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
